package com.ahdms.es.engine;

import com.ahdms.es.bean.SesSignInfo;
import com.ahdms.es.bean.enums.FileType;
import com.ahdms.es.exceptions.ApiException;
import com.ahdms.es.exceptions.OFDVerifyException;
import com.ahdms.es.result.VerifyResult;

import java.io.IOException;
import java.util.List;

/**
 * @author qinxiang
 * @date 2021-04-14 15:00
 */
public abstract class FileVerifyEngine {

    public static FileVerifyEngine getInstance(byte[] fileData, String fileType) throws IOException {
        if (FileType.PDF.getType().equalsIgnoreCase(fileType)) {
            return new PdfFileVerifyEngine(fileData);
        } else if (FileType.OFD.getType().equalsIgnoreCase(fileType)) {
            return new OfdFileVerifyEngine(fileData);
        } else {
            throw new ApiException("不支持的文件类型..");
        }
    }

    public static List<VerifyResult<SesSignInfo>> verify(byte[] fileData, String fileType) {
        try {
            return getInstance(fileData,fileType).verify();
        } catch (IOException e) {
            throw new ApiException("文件数据有误,解析失败..");
        } catch (OFDVerifyException e) {
            throw new ApiException(e.getMessage());
        }
    }

    public abstract List<VerifyResult<SesSignInfo>> verify() throws OFDVerifyException;

}
